/***********************************************************************
    filename:   DaterCalendar.h
    created:    10/4/2012
    author:     4Ys

    purpose:    Implements base class for Calendar objects
*************************************************************************/

#include "DaterCalendarData.h"

#include <time.h>

namespace Dater
{
	//----------------------------------------------------------------------------//
	CalendarData::CalendarData()
	{
		// Get the current time
		tm* temTm = new tm();
		time_t temTime = time(0);
		localtime_s(temTm, &temTime);

		int currentYear = temTm->tm_year + 1900;

		delete temTm;

		CalendarYear* commonYP;

		for(int i = -50; i <= 10; i++)
		{
			commonYP = new CalendarYear(currentYear + i);
			_years[currentYear + i] = commonYP;
		}
	}

	//----------------------------------------------------------------------------//
	CalendarData::~CalendarData()
	{
		CalendarYear* commonYP;

		std::map<int, CalendarYear*>::iterator calendarIter;
		for(calendarIter = _years.begin(); calendarIter != _years.end(); calendarIter++)
		{
			commonYP = calendarIter->second;
			delete commonYP;
		}
	}

	//----------------------------------------------------------------------------//
	CalendarYear* CalendarData::getYearCalendar(int year)
	{
		if(_years.count(year))
			return _years[year];

		return NULL;
	}

	//----------------------------------------------------------------------------//
	void CalendarData::addEvent(Event* _event)
	{
		int s_year = _event->getStartTime().getTime()->tm_year + 1900;
		int e_year = _event->getFinishTime().getTime()->tm_year + 1900;

		while(s_year <= e_year)
		{
			_years[s_year]->addEvent(_event);
			s_year++;
		}
	}

	//----------------------------------------------------------------------------//
	void CalendarData::deleteEvent(Event* _event)
	{
		int s_year = _event->getStartTime().getTime()->tm_year + 1900;
		int e_year = _event->getFinishTime().getTime()->tm_year + 1900;

		while(s_year <= e_year)
		{
			_years[s_year]->deleteEvent(_event);
			s_year++;
		}
	}
}